package com.zscat.mallplus.controller;

import com.zscat.mallplus.entity.vo.pms.PmsProductFootprintVo;
import com.zscat.mallplus.service.pms.service.IPmsFootprintService;
import com.zscat.mallplus.service.ums.service.IUmsMemberService;
import com.zscat.mallplus.ums.entity.UmsMember;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.vo.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;

/**
 * @Author ZuRongTang
 * @Date 2020/3/2
 **/

@RestController
@RequestMapping("/api/pms/my")
@Api(description = "足迹相关")
public class PmsFootprintController {

    @Resource
    private IUmsMemberService memberService;

    @Resource
    private IPmsFootprintService iPmsFootprintService;


    @ApiOperation("添加我的足迹")
    @RequestMapping(value = "/addFootprint", method = RequestMethod.GET)
    public Object addFootprint(Long productId) {
        CommonResult commonResult = new CommonResult();

        if (productId == null) {
            return commonResult.paramFailed();
        }

        Long memberId = memberService.getNewCurrentMember().getId();

        if (memberId == null) {
            return commonResult.failed("请登录");
        }
        return iPmsFootprintService.addFootprint(memberId, productId);

    }

    @ApiOperation("获取我的足迹列表")
    @RequestMapping(value = "/getMyFootprintList", method = RequestMethod.GET)
    public Object getMyFootprintList(Integer pageSize, Integer pageIndex) throws ParseException {
        CommonResult commonResult = new CommonResult();

        Long memberId = memberService.getNewCurrentMember().getId();

        if (memberId == null) {
            return commonResult.failed("请登录");
        }

        PageInfo<PmsProductFootprintVo> myFootprintList = iPmsFootprintService.getMyFootprintList(memberId, pageSize, pageIndex);

        if (myFootprintList == null) {
            return commonResult.success("暂无足迹", null);
        }
        return commonResult.success(myFootprintList);
    }


    @ApiOperation("删除我的足迹")
    @RequestMapping(value = "/removeMyFootprint", method = RequestMethod.GET)
    public Object removeMyFootprint(String id) {
        CommonResult commonResult = new CommonResult();

        Long memberId = memberService.getNewCurrentMember().getId();

        if (memberId == null) {
            return commonResult.failed("请登录");
        }

        if (StringUtils.isEmpty(id)) {
            return commonResult.paramFailed();
        }
        iPmsFootprintService.removeMyFootprint(id);
        return commonResult.success();
    }


}
